import request from '@/base/request';
import { ref } from 'vue';
import {ElMessage} from  'element-plus'
import router from '@/router';
 
export default function(){
    const step = ref(0);
    const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
    const testId=ref();
    const textParmInput=ref({
        testName:"测验："+Date.now(),
        time:60
    })
    const processSteps = async () => {
        step.value += 1;
        if(step.value==3)return;

        if(step.value==4){
            if(textParmInput.value.testName.length<1){
                ElMessage({
                    message:`测验名请勿为空`,
                    grouping:true,
                    type:'error'
                })
                step.value--;
                return
            }
            if(textParmInput.value.time<1 || textParmInput.value.time>24*60){
                ElMessage({
                    message:`时间最少为1分钟,最多一天`,
                    grouping:true,
                    type:'error'
                })
                step.value--;
                return
            }
            const response= (await request.post("/problem/test/createTest",textParmInput.value)).data
            if(response.code!==200){
                step.value=20
                ElMessage({
                    message:`创建失败${response.msg}`,
                    grouping:true,
                    type:'error'
                })
            }else{
                testId.value=response.data
            }
        }
        if (step.value < 10) {
            await delay(step.value<6?1500:900);
            processSteps();
        }else{
            router.push(`/testing/${testId.value}`)
        }
        
    };


    return {textParmInput,step,processSteps}
}